/**
 * Created by jocky on 07/10/2017.
 */

var withdrawObj = {
    /**
     * 提现历史
     */
    lists: function (type, direction) {
        //显示loading
        show_loading_table('withdraw_history_table', 6);

        var params = {};
        params.isRender = 2;//ajax调用

        var pageNo_dom = 'withdraw_history_pageNo';
        var pageTotal_dom = 'withdraw_history_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        var formObj = $('#withdraw_history_search_form');
        $.each(formObj.find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/withdraw/lists', params, function (json) {
            var t = json;
            $("#" + pageNo_dom).val(parseInt(t.pageNo));
            $("#" + pageTotal_dom).html(parseInt(t.pageTotal));
            var sumMoney = parseFloat(t.sumMoney).toFixed(2);

            var tr_head = "<tr>" +
                "<td><i style='color: red;'>汇总</i></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td class='text-right'><i>" + sumMoney + "</i></td>";
            if (t.is_first) {
                tr_head += "<td></td>";
            }
            tr_head += "</tr>";

            var tr = "";
            $.each(t.lists, function (i, v) {
                var status;
                var operate = "  ";
                switch (parseInt(v.status)) {
                    case 0:
                        status = "<label class='label label-warning'>未处理</label>";
                        operate = "" +
                            "<a href='javascript:void(0)' onclick='withdrawObj.confirmForm(" + v.id + ")'>同意</a>" +
                            " | " +
                            "<a href='javascript:void(0)' onclick='withdrawObj.cancel(" + v.id + ")'>取消</a>";
                        break;
                    case 1:
                        status = "<label class='label label-default'>请求中</label>";
                        break;
                    case 2:
                        status = "<label class='label label-danger'>失败 | " + v.resp_code + "</label>";
                        break;
                    case 3:
                    case 6:
                        status = "<label class='label label-success'>成功</label>";
                        break;
                    case 4:
                        status = "<label class='label label-default'>平台取消</label>";
                        break;
                    case 5:
                        status = "<label class='label label-default'>用户取消</label>";
                        break;
                    default:
                        status = "";
                        operate = " / ";
                        break;
                }
                tr += "<tr>";
                tr += "<td><a href='javascript:void(0)' title='查看 / 修改账户信息' onclick=\"traderObj.setForm(" + v.userID + ")\">" + v.loginName + "</a></td>";
                tr += "<td>" + v.userID + "</td>";
                tr += "<td class='text-center'>" + convertDate(v.withdraw_open_time, 1) + "</td>";
                tr += "<td>" + status + "</td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.money) + "</i></td>";
                tr += "<td class='text-center'>" + operate + "</td>";
                tr += "</tr>";
            });

            $("#withdraw_history_table").find('tbody').html(tr_head + tr);
        })
    },

    /**
     * 同意出金
     * @param id
     */
    confirmForm: function (id) {
        $.get('/withdraw/confirm-form', {id: id}, function (html) {
            $('#modal_pop_title').html('<i>确认出金</i>');
            $(document).off('click', '#modal_pop_btn');
            $(document).on('click', '#modal_pop_btn', function () {
                withdrawObj.confirm();
            });

            $('#modal_pop_body').html(html);
            $('#modal_pop').modal('show');
        })
    },

    /**
     * 同意出金
     */
    confirm: function () {
        var formObj = $('#money_withdraw_operate');
        var params = {};
        $.each(formObj.find('input,select,textarea'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.post('/withdraw/confirm', params, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };

            if (json.code == 1) {
                obj.type = 'info';
                obj.addclass = 'dark';

                $('#modal_pop').modal('hide');
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);

            withdrawObj.lists(3, 0);
        }, 'json')
    },

    /**
     * 取消
     * @param id
     */
    cancel: function (id) {
        swal({
            title: '确定取消该笔提现吗？',
            text: " ",
            type: 'warning',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            confirmButtonText: '确定'
        }).then(function () {
            $.getJSON('/withdraw/cancel', {id: id}, function (json) {
                var obj = {
                    title: '操作通知',
                    text: json.msg,
                    styling: 'bootstrap3',
                    delay: 5000,
                    type: ''
                };
                if (json.code == 1) {
                    obj.type = 'info';
                    obj.addclass = 'dark';
                } else {
                    obj.type = 'error';
                }
                new PNotify(obj);

                withdrawObj.lists(3, 0);
            })
        }).catch(swal.noop);
    }
};